package cn.bytecollege.controller;

import cn.bytecollege.entity.Admin;
import cn.bytecollege.service.AdminService;
import cn.bytecollege.util.MD5Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 修改管理员的名字和密码
 * @author qy
 */
@WebServlet(name = "AdminUpdateController", value = "/admin/updateAdmin")
public class AdminUpdateController extends HttpServlet {
    AdminService adminService = new AdminService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String id = request.getParameter("id");

        //封装数据
        Admin admin = new Admin();
        admin.setAdminName(username);
        admin.setAdminPassword(MD5Util.md5Encode(password));
        admin.setAdminId(Integer.valueOf(id));
        adminService.update(admin);

        //修改成功，跳转到登录界面重新登录
        response.sendRedirect(request.getContextPath()+"/admin/login");
    }
}
